package com.jiajia.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiajia.server.mapper.MenuMapper;
import com.jiajia.server.pojo.Admin;
import com.jiajia.server.pojo.Menu;
import com.jiajia.server.service.IMenuService;
import com.jiajia.server.utils.AdminUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiajia
 * @since 2021-08-10
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

        @Autowired
        private MenuMapper menuMapper;
        @Autowired
        private RedisTemplate redisTemplate;

    /**
     * 通过用户id查询菜单列表
     * @return
     */
    @Override
    public List<Menu> getMenusByAdminId() {
        Integer userid = AdminUtils.getAdmin().getId();
        ValueOperations valueOperations = redisTemplate.opsForValue();
        List<Menu> menus = (List<Menu>) valueOperations.get("munt_" + userid);
        //如果为空
        if (CollectionUtils.isEmpty(menus)){
            //数据库娶
            System.out.println("数据库！");
            menus = menuMapper.getMenusByAdminId(userid);
            //放进redis
            valueOperations.set("munt_" + userid,menus);

        }


        return     menus;

    }

    /**
     *  根据角色获取菜单列表
     * @return
     */
    @Override
    public List<Menu> getMenusFromRole() {

        return menuMapper.getMenusFromRole();
    }

    /**
     * 查询所有菜单
     * @return
     */
    @Override
    public List<Menu> getAllMenus() {
        return menuMapper.getAllMenus();
    }
}
